﻿using System.Collections.Generic;
using System.Threading.Tasks;
using GrapeCity.Forguncy.Commands;
using TencentCloud.Common;
using TencentCloud.Ocr.V20181119.Models;
using TencentCloudOcrPlugin.Resources;

namespace TencentCloudOcrPlugin.Ocr
{
    public class OcrBusinessCardAction : OcrActionBase
    {
        public override List<string> GetParamList(TencentCloudApiPropertyBase properties)
        {
            var list = base.GetParamList(properties);
            list.Add("姓名");
            list.Add("英文姓名");
            list.Add("英文地址");
            list.Add("公司");
            list.Add("英文公司");
            list.Add("职位");
            list.Add("英文职位");
            list.Add("部门");
            list.Add("英文部门");
            list.Add("手机");
            list.Add("电话");
            list.Add("传真");
            list.Add("社交帐号");
            list.Add("QQ");
            list.Add("MSN");
            list.Add("微信");
            list.Add("微博");
            list.Add("邮箱");
            list.Add("邮编");
            list.Add("网址");
            list.Add("公司账号");
            list.Add("其他");
            return list;
        }

        public override async Task<Dictionary<string,object>> RunAsync(TencentCloudApiClientConfig clientConfig,
            IServerCommandExecuteContext dataContext, TencentCloudApiPropertyBase properties)
        {
            var businessCardProperties = properties as OcrBusinessCardProperties;
            var baseRequest = await BuildRequestAsync(businessCardProperties, dataContext);
            var request = new BusinessCardOCRRequest()
            {
                ImageBase64 = baseRequest.ImageBase64,
                ImageUrl = baseRequest.ImageUrl
            };
            var response = await GetClient(clientConfig).BusinessCardOCR(request);
            return BuildResp(response, properties);
        }

        private Dictionary<string, object> BuildResp(AbstractModel tencentResp, TencentCloudApiPropertyBase properties) {
            var dict = new Dictionary<string, object>();
            var businessOcrResp = tencentResp as BusinessCardOCRResponse;
            if (businessOcrResp == null)
            {
                return dict;
            }
            if (properties.advancedSettings.TencentRawResp)
            {
                dict["原始数据"] = tencentResp;
            }
            var paramList = GetParamList(properties);
            var dataDict = new Dictionary<string, string>();
            foreach (var businessCardInfo in businessOcrResp.BusinessCardInfos)
            {
                var name = businessCardInfo.Name;
                var value = businessCardInfo.Value;
                if (!dataDict.TryAdd(name, value))
                {
                    dataDict[name] = dataDict[name] + " " + value;
                }
            }
            foreach (var name in paramList)
            {
                dataDict.TryGetValue(name, out var value);
                dict[name] = value;
            }
            return dict;
        }
    }
}